/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package hu.szitar.forum.client;

import hu.szitar.forum.common.ForumEngine;
import hu.szitar.forum.common.model.User;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author tircsil
 */
public class ForumClient {

    private static ForumClient instance;
    private ForumEngine stub;

    public static ForumClient getInstance() {
        if (instance == null) {
            instance = new ForumClient();
        }
        return instance;
    }

    private ForumClient() {
        initClient();
    }

    public static void main(String args[]) {
        //initClient();
    }

    private void initClient() {
        /*if (System.getSecurityManager() == null) {
         System.setSecurityManager(new SecurityManager());
         }*/
        try {
            String name = "Compute";
            Registry registry = LocateRegistry.getRegistry(1099);//TODO ide jönne a port szám
            stub = (ForumEngine) registry.lookup(name);
        } catch (Exception e) {
            System.err.println("ComputePi exception:");
            e.printStackTrace();
        }
    }

    public User login(String userName, String password) {
        User user = new User();
        user.setUserName(userName);
        Login task = new Login(user);
        try {
            user = stub.executeTask(task);
        } catch (RemoteException ex) {
            ex.printStackTrace();
            user = null;
        }
        return user;
    }
}
